package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.TransportInfo;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.TransportInfoRepository;
import co.yixiang.modules.shop.service.TransportInfoService;
import co.yixiang.modules.shop.service.dto.TransportInfoDto;
import co.yixiang.modules.shop.service.dto.TransportInfoQueryCriteria;
import co.yixiang.modules.shop.service.mapper.TransportInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-08
*/
@Service
//@CacheConfig(cacheNames = "transportInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TransportInfoServiceImpl implements TransportInfoService {

    private final TransportInfoRepository transportInfoRepository;

    private final TransportInfoMapper transportInfoMapper;

    public TransportInfoServiceImpl(TransportInfoRepository transportInfoRepository, TransportInfoMapper transportInfoMapper) {
        this.transportInfoRepository = transportInfoRepository;
        this.transportInfoMapper = transportInfoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(TransportInfoQueryCriteria criteria, Pageable pageable){
        Page<TransportInfo> page = transportInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(transportInfoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<TransportInfoDto> queryAll(TransportInfoQueryCriteria criteria){
        return transportInfoMapper.toDto(transportInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public TransportInfoDto findById(Integer id) {
        TransportInfo transportInfo = transportInfoRepository.findById(id).orElseGet(TransportInfo::new);
        ValidationUtil.isNull(transportInfo.getId(),"TransportInfo","id",id);
        return transportInfoMapper.toDto(transportInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public TransportInfoDto create(TransportInfo resources) {
        return transportInfoMapper.toDto(transportInfoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(TransportInfo resources) {
        TransportInfo transportInfo = transportInfoRepository.findById(resources.getId()).orElseGet(TransportInfo::new);
        ValidationUtil.isNull( transportInfo.getId(),"TransportInfo","id",resources.getId());
        transportInfo.copy(resources);
        transportInfoRepository.save(transportInfo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            transportInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TransportInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransportInfoDto transportInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", transportInfo.getUserId());
            map.put("标题", transportInfo.getTitle());
            map.put("手机号", transportInfo.getPhone());
            map.put("品牌id", transportInfo.getBrandId());
            map.put("类型id", transportInfo.getCateId());
            map.put("起点，省", transportInfo.getProvinceBegin());
            map.put("起点，市", transportInfo.getCityBegin());
            map.put("起点，区", transportInfo.getDistrictBegin());
            map.put("起点，具体地址", transportInfo.getDetailBegin());
            map.put("终点，省", transportInfo.getProvinceEnd());
            map.put("终点，市", transportInfo.getCityEnd());
            map.put("终点，区", transportInfo.getDistrictEnd());
            map.put("终点，具体地址", transportInfo.getDetailEnd());
            map.put("详细信息", transportInfo.getMessage());
            map.put("图片", transportInfo.getImage());
            map.put("阅读次数", transportInfo.getCountNumber());
            map.put("创建时间", transportInfo.getCreateTime());
            map.put("是否删除", transportInfo.getDelFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}